// 引入封装好的查询数据库方法
const readData = require("../db/module/readData");

// 封装函数 第一个参数是需要查询数据字典的字段和数据字典标识 ， 第二个字段是需要处理的数据
const disposeDatabase = function (database, data) {
  return new Promise(async (resolve, reject) => {
    if (database && data) {
      for (let i = 0; i < data.length; i++) {
        let item = data[i];

        for (let i = 0; i < database.length; i++) {
          let db = database[i];
          let result = await readData(
            {
              page: 1,
              size: 1000,
              dataBaseCode: db.dataBaseCode,
              code: item[db.key],
            },
            "dictionaryItem"
          );
          let keyName = db.key + "Name";
          if (result.data.length != 0) {
            item[keyName] = result.data[0].name;
          }
        }
      }

      resolve(data);
    }
  });
};

module.exports = { disposeDatabase };
